#include "dswosd.h"

CDswosd::CDswosd(LPST_ENV_T pEnv, void* pCapture, int nCh)
: m_pEnv(pEnv)
, m_pCapture(pCapture)
, m_nCh(nCh)
, m_nOldTime(0)
{
	LOG4CPLUS_DEBUG(log_dsw, "Create OSD on channel " << nCh);

	m_nTimer.ev = NULL;
	
	int nChID = nCh;
	char sOsd[32];
	void* pParam = (void*)&nChID;

	m_pEnv->pOnNotifyFunc(PMSG_GET_VIDEOINFO, pParam);
	if (!((LPST_CH_PARAM)pParam)->osd.enosd)
		return;
	strcpy(sOsd, ((LPST_CH_PARAM)pParam)->osd.text);

	 ShowTextOsd(sOsd);
	
	m_nTimer.ev = evtimer_new(m_pEnv->pEvBase, CDswosd::cbTimerTask, this);
	m_nTimer.tv.tv_sec = 0;
	m_nTimer.tv.tv_usec = 1000 * 200;
	evtimer_add(m_nTimer.ev, &m_nTimer.tv);
}

CDswosd::~CDswosd()
{
	CLEAR_EV(m_nTimer);
	LOG4CPLUS_DEBUG(log_dsw, "Destroy OSD!");
}

void CDswosd::ShowTextOsd(char* pText)
{
	gm_osd_font_t osd_font;
	int i;

	osd_font.enabled = 1;
	osd_font.win_idx = 0;
	osd_font.align_type = GM_OSD_ALIGN_TOP_LEFT;
	osd_font.x = 10;
	osd_font.y = 10;
	osd_font.h_words = strlen(pText);
	osd_font.v_words = 1;
	osd_font.h_space = 0;
	osd_font.v_space = 0;
	osd_font.font_index_len = strlen(pText);
	for (i = 0; i < osd_font.font_index_len; i++)
		osd_font.font_index[i] = pText[i];//(unsigned short)(FONT_INDEX_STR + FONT_MAX_NUM * m_nCh + i);
	osd_font.font_alpha = GM_OSD_FONT_ALPHA_75;
	osd_font.win_alpha = GM_OSD_FONT_ALPHA_75;
	osd_font.win_palette_idx = 0;
	osd_font.font_palette_idx = 1;
	osd_font.priority = GM_OSD_PRIORITY_MARK_ON_OSD;
	osd_font.smooth.enabled = 1;
	osd_font.smooth.level = GM_OSD_FONT_SMOOTH_LEVEL_WEAK;
	osd_font.marquee.mode = GM_OSD_MARQUEE_MODE_NONE;
	osd_font.border.enabled = 0;
	osd_font.border.width = 1;
	osd_font.border.type = GM_OSD_BORDER_TYPE_WIN;
	osd_font.border.palette_idx = 3;
	osd_font.font_zoom = GM_OSD_FONT_ZOOM_NONE;

	gm_set_osd_font(m_pCapture, &osd_font);
}

void CDswosd::UpdateTimeOsd(time_t nNow)
{
	char curtime[64];
	strftime(curtime, sizeof(curtime), "%Y-%m-%d %H:%M:%S", localtime(&nNow));
	gm_osd_font_t osd_font;
	int i;

	osd_font.enabled = 1;
	osd_font.win_idx = 1;
	osd_font.align_type = GM_OSD_ALIGN_TOP_RIGHT;
	osd_font.x = 10;
	osd_font.y = 10;
	osd_font.h_words = strlen(curtime);
	osd_font.v_words = 1;
	osd_font.h_space = 0;
	osd_font.v_space = 0;
	osd_font.font_index_len = strlen(curtime);
	for (i = 0; i < osd_font.font_index_len; i++){
		int nindex = 0;
		if (curtime[i] == '-')
			nindex = 10;
		else if (curtime[i] == ':')
			nindex = 11;
		else if (curtime[i] == ' ')
			nindex = 12;
		else
			nindex = curtime[i] - 0x30;
		osd_font.font_index[i] = curtime[i];//(unsigned short)nindex;
	}
	osd_font.font_alpha = GM_OSD_FONT_ALPHA_75;
	osd_font.win_alpha = GM_OSD_FONT_ALPHA_75;
	osd_font.win_palette_idx = 0;
	osd_font.font_palette_idx = 1;
	osd_font.priority = GM_OSD_PRIORITY_MARK_ON_OSD;
	osd_font.smooth.enabled = 1;
	osd_font.smooth.level = GM_OSD_FONT_SMOOTH_LEVEL_WEAK;
	osd_font.marquee.mode = GM_OSD_MARQUEE_MODE_NONE;
	osd_font.border.enabled = 0;
	osd_font.border.width = 1;
	osd_font.border.type = GM_OSD_BORDER_TYPE_WIN;
	osd_font.border.palette_idx = 3;
	osd_font.font_zoom = GM_OSD_FONT_ZOOM_NONE;

	gm_set_osd_font(m_pCapture, &osd_font);
}

void CDswosd::OnTimer(int fd, short events)
{
	time_t now = time(NULL);
	if (m_nOldTime != now){
		UpdateTimeOsd(now);
		m_nOldTime = now;
	}
	m_nTimer.tv.tv_usec = 1000 * 200;
	evtimer_add(m_nTimer.ev, &m_nTimer.tv);
}

